﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class GenerateReportsDialog : ChildWindow
    {
        List<CheckBox> checkBoxList = new List<CheckBox>();

        public GenerateReportsDialog()
        {
            InitializeComponent();
        }

        public async void init()
        {
            // Get reports
            await Globals.db.GetReportObjects("1=1 order by Name");
            dpReportDate.SelectedDate = DateTime.Now;
            // Load list boxes
            lbDailyReports.Items.Clear();
            lbWeeklyReports.Items.Clear();
            lbMonthlyReports.Items.Clear();
            checkBoxList.Clear();
            foreach (ServiceReferenceOpenRPOS.Report report in Globals.db.ReportRecords)
            {
                CheckBox cb = new CheckBox();
                cb.Content = report.col_Name;
                cb.Padding = new Thickness(3, -3, 0, 0);
                cb.IsChecked = true;
                checkBoxList.Add(cb);
                if (report.col_Frequency == "D")
                    lbDailyReports.Items.Add(cb);
                else if (report.col_Frequency == "W")
                    lbWeeklyReports.Items.Add(cb);
                else
                    lbMonthlyReports.Items.Add(cb);
            }
        }

        private async void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Globals.reports.initReportNameComboBox();
            // Flag the checked reports for generation
            int i = 0;
            foreach (ServiceReferenceOpenRPOS.Report report in Globals.db.ReportRecords)
            {
                if (checkBoxList[i++].IsChecked==true)
                {
                    report.bFilterGenerate = true;
                    report.col_Generate = true;
                    report.bFilterGeneratedByLoginID = true;
                    report.col_GeneratedByLoginID = Globals.loginID;
                    await Globals.db.UpdateReportObject(report);
                }
            }
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Globals.reports.initReportNameComboBox();
            this.DialogResult = false;
        }

        private void btnSelectAllDaily_Click(object sender, RoutedEventArgs e)
        {
            // Select all of the daily checkboxes
            foreach (CheckBox cb in lbDailyReports.Items)
                cb.IsChecked = true;
        }

        private void btnClearAllDaily_Click(object sender, RoutedEventArgs e)
        {
            // Clear all of the daily checkboxes
            foreach (CheckBox cb in lbDailyReports.Items)
                cb.IsChecked = false;
        }

        private void btnSelectAllWeekly_Click(object sender, RoutedEventArgs e)
        {
            // Select all of the weekly checkboxes
            foreach (CheckBox cb in lbWeeklyReports.Items)
                cb.IsChecked = true;
        }

        private void btnClearAllWeekly_Click(object sender, RoutedEventArgs e)
        {
            // Clear all of the weekly checkboxes
            foreach (CheckBox cb in lbWeeklyReports.Items)
                cb.IsChecked = false;
        }

        private void btnSelectAllMonthly_Click(object sender, RoutedEventArgs e)
        {
            // Select all of the monthly checkboxes
            foreach (CheckBox cb in lbMonthlyReports.Items)
                cb.IsChecked = true;
        }

        private void btnClearAllMonthly_Click(object sender, RoutedEventArgs e)
        {
            // Clear all of the monthly checkboxes
            foreach (CheckBox cb in lbMonthlyReports.Items)
                cb.IsChecked = false;
        }
    }
}

